package com.example.clockin;

import com.example.clockin.JavaUi.JavaUi;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.ace.ability.AceInternalAbility;
import ohos.app.AbilityContext;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;

public class set_profileServiceAbility extends AceInternalAbility {
    /**
     * 定义状态码
     */
    private static final Integer ERROR = -1;
    private static final Integer SUCCESS = 0;

    /**
     * 标识，用于与前端操作类型匹配，类似MVC的接口路径
     */
    private static final Integer PLUS = 1001;

    /**
     * 定义一个类对象，用于登记、注销使用
     */
    private static set_profileServiceAbility Set_ProfileServiceAbility;

    /**
     * 定义程序上下文类
     */
    private AbilityContext abilityContext;

    private static Ability ability;
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    public set_profileServiceAbility(String bundleName, String abilityName) {
        super(bundleName, abilityName);
    }

    public set_profileServiceAbility(String abilityName) {
        super(abilityName);
    }

    /**
     * 提供PA注册登记方法，在继承了AceAbility的类中onStart方法中调用
     *
     * @param abilityContext        传入继承AceAbility类的对象（this）
     */
    public static void register(AbilityContext abilityContext, String bundleName, String abilityName, Ability ability){
        Set_ProfileServiceAbility.ability = ability;
        // 初始化类对象
        Set_ProfileServiceAbility = new set_profileServiceAbility(bundleName, abilityName);
        Set_ProfileServiceAbility.abilityContext = abilityContext;
        Set_ProfileServiceAbility.setInternalAbilityHandler((code, data, reply, option) -> {
            return Set_ProfileServiceAbility.onRemoteRequest(code, data, reply, option);
        });
    }

    /**
     * 提供PA注销方式，在继承了AceAbility的类中onStop方法内调用
     */
    public static void deRegister(){
        Set_ProfileServiceAbility.abilityContext = null;
        Set_ProfileServiceAbility.setInternalAbilityHandler(null);
    }

    public boolean onRemoteRequest(Integer code, MessageParcel data, MessageParcel reply, MessageOption option){
        //  判断类型是否匹配
        System.out.println("进入java");
        if(PLUS.equals(code)){
            System.out.println("我要跳转了");
            return this.indexJsAbility(data, reply, option);
        }

        return true;
    }

    private boolean indexJsAbility(MessageParcel data, MessageParcel reply, MessageOption option){
        System.out.println("跳转方式");
        Intent intent = new Intent();
        // 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
        Operation operation = new Intent.OperationBuilder()
//                .withDeviceId("")
                // config文件中的bundleName 和 abilityName
//                .withBundleName("com.example.clockin")
//                .withAbilityName("com.example.clockin.secondAbility")
                .withBundleName(ability.getBundleName())
                .withAbilityName(JavaUi.class.getName())
//                .withAction("action.call_Album")
//                .withAction(Intent.ACTION_set_Profile)
                .build();
        // 把operation设置到intent中
        intent.setOperation(operation);
        System.out.println("我是intentaaaaaaaaaaaaaaa:" + intent);
        // 跳转到java
        ability.startAbility(intent);
        System.out.println("跳转结束");
        return true;
    }



}

